package cn.wenhe9.yygh.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;


/**
 * @author DuJinliang
 * 2022/2/11
 * 全局统一返回结果类
 */
@Data
@ApiModel(value = "全局统一返回结果")
public class ResultResponse<T> {

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public ResultResponse(){}

    protected static <T> ResultResponse<T> build(T data) {
        ResultResponse<T> ResultResponse = new ResultResponse<T>();
        if (data != null) {
            ResultResponse.setData(data);
        }
        return ResultResponse;
    }

    public static <T> ResultResponse<T> build(T body, ResultCodeEnum ResultCodeEnum) {
        ResultResponse<T> ResultResponse = build(body);
        ResultResponse.setCode(ResultCodeEnum.getCode());
        ResultResponse.setMessage(ResultCodeEnum.getMessage());
        return ResultResponse;
    }

    public static <T> ResultResponse<T> build(Integer code, String message) {
        ResultResponse<T> ResultResponse = build(null);
        ResultResponse.setCode(code);
        ResultResponse.setMessage(message);
        return ResultResponse;
    }

    public static<T> ResultResponse<T> ok(){
        return ResultResponse.ok(null);
    }

    /**
     * 操作成功
     * @param data
     * @param <T>
     * @return
     */
    public static<T> ResultResponse<T> ok(T data){
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static<T> ResultResponse<T> fail(){
        return ResultResponse.fail(null);
    }

    /**
     * 操作失败
     * @param data
     * @param <T>
     * @return
     */
    public static<T> ResultResponse<T> fail(T data){
        return build(data, ResultCodeEnum.FAIL);
    }

    public ResultResponse<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public ResultResponse<T> code(Integer code){
        this.setCode(code);
        return this;
    }

    public boolean isOk() {
        if(this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()) {
            return true;
        }
        return false;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
